﻿using System;
using System.Xml.Linq;
using System.Windows.Forms;
using SedionFormDesigner.Serializer.BasicStruct;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.ComponentModel;
using System.Collections;
using CommonLibrary.DataModel.Form;

namespace SedionFormDesigner.Serializer
{
    public class FormSerializer
    {
        //public static XElement SerializeControlToXml(TabControl tabForms, String formName)
        //{
        //    return SeriCtrlAndXml.Instance.ControlToXml(tabForms, formName);
        //}

        public static XElement SerializeControlToXml(Control form, Int32 index)
        {
            return SeriCtrlAndXml.Instance.ControlToXml(form, index);
        }

        public static FormModel SerializeXmlToControl(XElement xml)
        {
            FormModel model = new FormModel();
            model.Name = xml.Attribute("FormName").Value;
            var xmls = xml.Elements();
            foreach (var form in xmls)
            {
                model.Controls.AddRange(SeriCtrlAndXml.Instance.XmlToControl(form));
            }

            return model;
        }

        public static String SerializeXmlToControl(IDesignerLoaderHost host, XElement xml)
        {
            IComponent instance = host.CreateComponent(typeof(System.Windows.Forms.Form));
            String baseClassName = instance.Site.Name;

            PropertyDescriptor formProperty = TypeDescriptor.GetProperties(instance)["Controls"];
            Control.ControlCollection controls = formProperty.GetValue(instance) as Control.ControlCollection;
            controls.AddRange(SeriCtrlAndXml.Instance.XmlToControl(host, xml).ToArray());
            return baseClassName;
        }

        #region XmlToHtml

        public static XmlFromModelList SerializeXmlToHtml(XElement xml)
        {
            return SeriHtmlAndXml.Instance.XmlToHtml(xml);
        }

        public static XmlFromModelList SerializeXmlToHtml(XElement xml, List<FormValueEntities> formValueEntity, DataTable dt_spjg)
        {
            return SeriHtmlAndXml.Instance.XmlToHtmlReadonly(xml, formValueEntity, dt_spjg);
        }

        public static String ProduHtmlSelectOption(String option)
        {
            return SeriHtmlAndXml.Instance.ProduSelectOption(option);
        }

        public static String ProduHtmlTable(DataTable dt)
        {
            return SeriHtmlAndXml.Instance.ProduTable(dt);
        }

        #endregion

        #region XmlToEntity

        public static FormEntity SerializeXmlToFormEntity(XElement xml)
        {
            return SeriEntyAndXml.Instance.SerializeXmlToFormEntity(xml);
        }

        #endregion
    }
}
